package com.supor.aiot.common.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.common.R;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.base.WebKitModel;
import com.supor.aiot.common.databinding.ActivityWebViewBinding;
import com.supor.aiot.common.ui.view.LollipopFixedWebView;

import java.util.HashMap;

@Route(path = PathUrl.PATH_WEB_VIEW)
public class WebViewActivity extends BaseMVVMEnhanceActivity<WebKitModel, ActivityWebViewBinding> {

    private String url;
    //    public static final String BANNERURL = "BannerUrl";
    private ValueCallback<Uri> uploadMessage;
    private ValueCallback<Uri[]> uploadMessageAboveL;
    private final static int FILE_CHOOSER_RESULT_CODE = 10000;
    private final static int FILE_SELECT_CODEB = 1;

    @Override
    protected void initData() {
        super.initData();
        if (null != getIntent()) {
            url = getIntent().getStringExtra("URL_PATH");
        }

        bindingView.classifyToolbar.setIvLeftOnClickListener(view -> finish());
        bindingView.classifyToolbar.setTitle("社区");
        bindingView.classifyToolbar.setTitleTextSize(Util.sp2px(this, 17));
        bindingView.classifyToolbar.setTitleColor(0xFF333333);
//        String token = "";
//        if (TextUtils.isEmpty(token)) {
//            finish();
//            return;
//        }
//        url = ApiContact.MALL_URL + "/#/index?token=Bearers " + token;
//        url = "http://140.207.48.210:30083/supor-h5/#/pages/communityHome/talkDetail?e=176&f=1";
//        url = "https://www.baidu.com/";
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        WebSettings webSetting = bindingView.webView.getSettings();
        webSetting.setSavePassword(false);
        webSetting.setSaveFormData(false);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(false);
        webSetting.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSetting.setUseWideViewPort(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSetting.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSetting.setPluginState(WebSettings.PluginState.ON);
        webSetting.setSupportMultipleWindows(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        bindingView.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                url = "suporiot://linkPage/fl_mine/SettingActivity?key1=value1&key2=value2";
                if (!TextUtils.isEmpty(url)) {

                    Uri uri = Uri.parse(url);
                    if (null != uri && "suporiot".equals(uri.getScheme()) && "linkPage".equals(uri.getHost())) {
                        ARouter.getInstance().build(uri.getPath())
                                .withTransition(com.android.baseconfig.R.anim.activity_open_enter,
                                        com.android.baseconfig.R.anim.activity_open_exit)
                                .navigation(getActivity());
                    } else {
                        view.loadUrl(url);
                    }
                    return true;
                } else {
                    return false;
                }
            }
        });

        String cachePath = getFilesDir().getAbsolutePath() + "appcache";
        Log.d("GameInfoBean_debug", cachePath);
//        webSetting.setAppCachePath(this.getDir("appcache", 0).getPath());
        webSetting.setAppCachePath(cachePath);
//        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setDatabasePath(cachePath);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSetting.setAppCacheMaxSize(8 * 1024 * 1024);

        bindingView.webView.loadUrl(url);

    }

    @Override
    public boolean
    onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && bindingView.webView.canGoBack()) {
            bindingView.webView.goBack();//返回上个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);//退出H5界面
    }
}